package com.example.bookmanage.controller;

import com.example.bookmanage.domain.TableDataInfo;
import com.example.bookmanage.domain.user.SysUser;
import com.example.bookmanage.domain.user.dto.*;
import com.example.bookmanage.domain.user.vo.SysUserVO;
import com.example.bookmanage.enums.ResultCode;
import com.example.bookmanage.result.Result;
import com.example.bookmanage.service.SysUserService;
import com.example.bookmanage.service.OssService;
import com.example.bookmanage.utils.ThreadLocalUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "用户管理")
@RestController
@RequestMapping("/sysUser")
public class SysUserController extends BaseController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private OssService ossService;

    /**
     * 用户登录
     *
     * @param loginDTO 用户登录信息
     * @return 登录结果
     */
    @Operation(summary = "用户登录")
    @PostMapping("/login")
    public Result<?> login(@RequestBody LoginDTO loginDTO) {
        return Result.success(sysUserService.login(loginDTO.getUsername(), loginDTO.getPassword()));
    }

    /**
     * 用户列表
     * @param sysUserQueDTO 查询条件
     * @return 用户列表
     */
    @Operation(summary = "用户列表")
    @GetMapping("/list")
    public TableDataInfo list(@ParameterObject SysUserQueDTO sysUserQueDTO) {
        return getTableDataInfo(sysUserService.list(sysUserQueDTO));
    }

    /**
     * 添加用户
     * @param sysUserDTO 用户信息
     * @return 操作结果
     */
    @Operation(summary = "添加用户")
    @PostMapping("/add")
    public Result<?> add(@RequestBody SysUserDTO sysUserDTO) {
        return toResult(sysUserService.add(sysUserDTO));
    }

    /**
     * 更新用户
     * @param sysUserEditDTO 用户编辑 DTO 对象，包含用户 ID 和新的用户信息
     * @return 操作结果
     */
    @Operation(summary = "更新用户")
    @PutMapping("/update")
    public Result<?> update(@RequestBody SysUserEditDTO sysUserEditDTO) {
        return toResult(sysUserService.update(sysUserEditDTO));
    }

    /**
     * 删除用户
     *
     * @param userId 用户 ID
     * @return 操作结果
     */
    @Operation(summary = "删除用户")
    @DeleteMapping("/delete/{userId}")
    public Result<?> delete(@PathVariable Long userId) {
        return toResult(sysUserService.deleteById(userId));
    }

    /**
     * 更新用户状态
     *
     * @param sysUserUpStatusDTO 用户状态更新 DTO 对象，包含用户 ID 和新的状态信息
     * @return 操作结果
     */
    @Operation(summary = "更新用户状态")
    @PutMapping("/updateStatus")
    public Result<?> updateUserStatus(@RequestBody @Validated SysUserUpStatusDTO sysUserUpStatusDTO) {
        return toResult(sysUserService.updateUserStatus(sysUserUpStatusDTO));
    }

    /**
     * 上传头像
     *
     * @param file 文件对象
     * @return 上传结果
     */
    @Operation(summary = "上传头像")
    @PostMapping("/avatar")
    public Result<?> uploadAvatar(@RequestParam("avatar") MultipartFile file) {
        if (file.isEmpty()) {
            return Result.fail(ResultCode.FAILED);
        }

        // 检查文件类型
        String contentType = file.getContentType();
        if (contentType != null && !contentType.startsWith("image/")) {
            return Result.fail();
        }

        // 上传文件到OSS
        String avatarUrl = ossService.uploadFile(file, "avatar");

        Long userId = ThreadLocalUtil.get("userId", Long.class);
        SysUser sysUser = new SysUser();
        sysUser.setUserId(userId);
        sysUser.setImgPath(avatarUrl);

        // 更新用户头像URL
        sysUserService.updateByImg(sysUser);

        return Result.success(avatarUrl);
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @Operation(summary = "获取用户信息")
    @GetMapping("/info")
    public Result<SysUserVO> getUserInfo() {
        return Result.success(sysUserService.getUserInfo());
    }
}
